package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

import static com.sky.constant.MessageConstant.GET_SHOP_STATUS_ERROR;
import static com.sky.constant.MessageConstant.SHOP_STATUS;

@RestController("adminShopController")
@Api(tags = "店铺相关接口")
@RequestMapping("/admin/shop")
@Slf4j
public class ShopController {

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 设置店铺营业状态
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置店铺营业状态")
    public Result setStatus(@PathVariable Integer status){
        log.info("店铺状态为：{}",status==1?"营业中":"打样中");
        redisTemplate.opsForValue().set(SHOP_STATUS,status);
        return Result.success();
    }

    /**
     * 查询店铺状态
     * @return
     */
    @GetMapping("/status")
    @ApiOperation("查询店铺状态")
    public Result<Integer> getStatus(){
        Integer status = (Integer) redisTemplate.opsForValue().get(SHOP_STATUS);
        // 将结果包装在 Optional 中
        Optional<Integer> optionalStatus = Optional.ofNullable(status);
        // 提供默认值
        Integer finalStatus = optionalStatus.orElse(0);
        log.info("店铺状态为：{}",finalStatus==1?"营业中":"打样中");
        return optionalStatus.map(Result::success).orElseGet(() -> Result.error(GET_SHOP_STATUS_ERROR));
    }
}
